﻿using AutoMapper;
using MediatR;
using Newtonsoft.Json;
using Production_Get.API.Infrastructures.Interfaces;
using Return.Enum;
using THE_WIND_RISES.Common.Domans.Produce.WorkCenter_;
using THE_WIND_RISES.Common.Dtos;
namespace Production_Get.API.Application.Commands.WorkCenter_W
{
    /// <summary>
    /// 工作中心显示列表
    /// </summary>
    public class WorkCenterShowHandler1 : IRequestHandler<WorkCenterShowCommand1, Result>
    {
        private readonly IBaseRepositoryGet<WorkCenter> WorkCenterRepository;
        private readonly ILogger<WorkCenterShowHandler1> logger;
        public WorkCenterShowHandler1(IBaseRepositoryGet<WorkCenter> workCenterRepository, ILogger<WorkCenterShowHandler1> logger)
        {
            WorkCenterRepository = workCenterRepository;
            this.logger = logger;
        }

        public async Task<Result> Handle(WorkCenterShowCommand1 request, CancellationToken cancellationToken)
        {
            var result = new THE_WIND_RISES.Common.Dtos.Result() { Code = (int)CommonErrorCodes.Added };
            try
            {
                Console.WriteLine("开始查询工作中心信息" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                Console.WriteLine(JsonConvert.SerializeObject(request));
                var sql = "select * from workCenters";
                var list = WorkCenterRepository.GetAll(sql, null).Where(x => x.IsDelWorkCenter == false);
                Console.WriteLine(sql);
                if (request.WorkCenterNum != "")
                {
                    list = list.Where(x => x.WorkCenterNum.Contains(request.WorkCenterNum));
                }
                if (request.WorkCenterName != "")
                {
                    list = list.Where(x => x.WorkCenterName.Contains(request.WorkCenterName));
                }
                if (request.WorkCenterType != "请选择")
                {
                    list = list.Where(x => x.WorkCenterType.Contains(request.WorkCenterType));
                }
                if (request.abc > 0)
                {
                    list = list.Where(x => x.WorkCenterState == request.WorkCenterState);
                }
                var totalcount = list.Count();
                Console.WriteLine(totalcount);
                var pagecount = Convert.ToInt32(Math.Ceiling(totalcount / 1.0 / request.pagesize));
                list = list.OrderBy(x => x.WorkCenterId).Skip((request.pageindex - 1) * request.pagesize).Take(request.pagesize);
                var data = new
                {
                    list,
                    totalcount,
                    pagecount
                };
                result.Code = (int)CommonErrorCodes.Success;
                result.Data = data;
                result.Msg = "";
                logger.LogInformation("工作中心显示列表查询成功");
            }
            catch (Exception ex)
            {
                logger.LogError($"工作中心显示列表查询失败," + ex.Message);
                throw;
            }
            return result;
        }
    }
}
